﻿
//***********************************************************************
// * Module:   TbBstFlfgHandler
// * 作者:  曹荣芝
// * Modified: 2013-05-06
// * Purpose: Definition of the Class TbBstFlfgHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.JWBST.JWBSTBLL;
using System.Collections;
using JWBSTAPP.CommonClass;
using XWG.Framework;
namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// TbBstFlfgHandler 的摘要说明
    /// </summary>
    public class TbBstFlfgHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {
                var mUser = AppOrganizatiOnUserBLL.Load(context.Session["UserID"].ToString());
                var mOrg = AppOrganizatiOnBLL.Load(mUser.OrgnaId);
                TbBstFlfg mTbBstFlfg = new TbBstFlfg();
                mTbBstFlfg.Flfgid = Guid.NewGuid().ToString(); //主键
                mTbBstFlfg.Syh = context.Request.Form["texSyh"]; //索引号
                mTbBstFlfg.Cjrq = DateTime.Now; //创建日期
                mTbBstFlfg.Yhzh = mUser.UserLoginname; //用户帐号
                mTbBstFlfg.Llr = mUser.UserName; //录入人
                mTbBstFlfg.Shr = context.Request.Form["texShr"]; //审核人
                mTbBstFlfg.Sfsh = context.Request.Form["texSfsh"]; //是否审核
                mTbBstFlfg.Lmbs = context.Request.Form["texLmbs"]; //栏目标识
                mTbBstFlfg.Ztflbm = context.Request.Form["texZtflbm"]; //主题分类编码
                mTbBstFlfg.Ztfl = context.Request.Form["texZtfl"]; //主题分类
                mTbBstFlfg.Fwdxfl = context.Request.Form["texFwdxfl"]; //服务对象分类
                if (string.IsNullOrEmpty(context.Request.Form["texXxsxh"]))
                {

                }
                else
                {
                    mTbBstFlfg.Xxsxh = Convert.ToDecimal(context.Request.Form["texXxsxh"]); //信息顺序号
                }
              
                mTbBstFlfg.Xxmc = context.Request.Form["texXxmc"]; //信息名称
                if (string.IsNullOrEmpty(context.Request.Form["texFwrq"]))
                {

                }
                else
                {
                    mTbBstFlfg.Fwrq = Convert.ToDateTime(context.Request.Form["texFwrq"]); //发文日期
                }
               
                mTbBstFlfg.Wjbh = context.Request.Form["texWjbh"]; //文件编号
                mTbBstFlfg.Zy = context.Request.Form["texZy"]; //摘要
                mTbBstFlfg.Ztc = context.Request.Form["texZtc"]; //主题词
                mTbBstFlfg.Cid = Convert.ToInt32(context.Request.Form["selCID"]); //分类
                mTbBstFlfg.Xxnr = context.Request.Form["texXxnr"]; //信息内容
                mTbBstFlfg.Gkfs = context.Request.Form["texGkfs"]; //公开方式
                mTbBstFlfg.Bgkly = context.Request.Form["texBgkly"]; //不公开理由
                mTbBstFlfg.Gksx = context.Request.Form["texGksx"]; //公开时限
                mTbBstFlfg.Gkfw = context.Request.Form["texGkfw"]; //公开范围
                mTbBstFlfg.Shcx = context.Request.Form["texShcx"]; //审核程序
                mTbBstFlfg.Zrbm = context.Request.Form["texZrbm"]; //责任部门
                if (string.IsNullOrEmpty(context.Request.Form["texKssj"]))
                {

                }
                else
                {
                    mTbBstFlfg.Kssj = Convert.ToDateTime(context.Request.Form["texKssj"]); //开始时间
                }
                if (string.IsNullOrEmpty(context.Request.Form["texJssj"]))
                {

                }
                else
                {
                    mTbBstFlfg.Jssj = Convert.ToDateTime(context.Request.Form["texJssj"]); //结束时间
                }
                mTbBstFlfg.Tcfl = context.Request.Form["texTcfl"]; //体裁分类
                mTbBstFlfg.Xgfj = context.Request.Form["texXgfj"]; //相关附件
                mTbBstFlfg.Zxljdz = context.Request.Form["texZxljdz"]; //在线链接地址
                //mTbBstFlfg.Cid = Convert.ToDecimal(context.Request.Form["texCid"]); //父级表相关联ID
                mTbBstFlfg.Sfsc = "0"; //是否删除 0 未删除  1删除
                mTbBstFlfg.Sfrd = context.Request.Form["selEnabled"]; //是否热点   0非热点 1热点 

                mResultModel = TbBstFlfgBLL.Insert(mTbBstFlfg, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                TbBstFlfg mTbBstFlfg = new TbBstFlfg();
                foreach (var paramKey in arryParamKey)
                {
                    mResultModel = TbBstFlfgBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); ; //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
        #endregion

        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            TbBstFlfg mTbBstFlfg = new TbBstFlfg();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mTbBstFlfg = TbBstFlfgBLL.Load(paramKey); //根据键值获取实体信息
                if (mTbBstFlfg != null)
                {
                    var mUser = AppOrganizatiOnUserBLL.Load(context.Session["UserID"].ToString());
                    var mOrg = AppOrganizatiOnBLL.Load(mUser.OrgnaId);
                    mTbBstFlfg.Syh = context.Request.Form["texSyh"]; //索引号
                    mTbBstFlfg.Yhzh = context.Request.Form["texYhzh"]; //用户帐号
                    mTbBstFlfg.Yhzh = mUser.UserLoginname; //用户帐号
                    mTbBstFlfg.Llr = mUser.UserName; //录入人
                    mTbBstFlfg.Sfsh = context.Request.Form["texSfsh"]; //是否审核
                    mTbBstFlfg.Lmbs = context.Request.Form["texLmbs"]; //栏目标识
                    mTbBstFlfg.Ztflbm = context.Request.Form["texZtflbm"]; //主题分类编码
                    mTbBstFlfg.Xgsj = DateTime.Now; //修改时间
                    mTbBstFlfg.Ztfl = context.Request.Form["texZtfl"]; //主题分类
                    mTbBstFlfg.Fwdxfl = context.Request.Form["texFwdxfl"]; //服务对象分类
                    mTbBstFlfg.Xxsxh = Convert.ToDecimal(context.Request.Form["texXxsxh"]); //信息顺序号
                    mTbBstFlfg.Xxmc = context.Request.Form["texXxmc"]; //信息名称
                    mTbBstFlfg.Fwrq = Convert.ToDateTime(context.Request.Form["texFwrq"]); //发文日期
                    mTbBstFlfg.Wjbh = context.Request.Form["texWjbh"]; //文件编号
                    mTbBstFlfg.Zy = context.Request.Form["texZy"]; //摘要
                    mTbBstFlfg.Ztc = context.Request.Form["texZtc"]; //主题词
                    mTbBstFlfg.Xxnr = context.Request.Form["texXxnr"]; //信息内容
                    mTbBstFlfg.Gkfs = context.Request.Form["texGkfs"]; //公开方式
                    mTbBstFlfg.Cid = Convert.ToInt32(context.Request.Form["selCID"]); //分类
                    mTbBstFlfg.Bgkly = context.Request.Form["texBgkly"]; //不公开理由
                    mTbBstFlfg.Gksx = context.Request.Form["texGksx"]; //公开时限
                    mTbBstFlfg.Gkfw = context.Request.Form["texGkfw"]; //公开范围
                    mTbBstFlfg.Shcx = context.Request.Form["texShcx"]; //审核程序
                    mTbBstFlfg.Zrbm = context.Request.Form["texZrbm"]; //责任部门
                    mTbBstFlfg.Kssj = Convert.ToDateTime(context.Request.Form["texKssj"]); //开始时间
                    mTbBstFlfg.Jssj = Convert.ToDateTime(context.Request.Form["texJssj"]); //结束时间
                    mTbBstFlfg.Tcfl = context.Request.Form["texTcfl"]; //体裁分类
                    mTbBstFlfg.Xgfj = context.Request.Form["texXgfj"]; //相关附件
                    mTbBstFlfg.Zxljdz = context.Request.Form["texZxljdz"]; //在线链接地址
                    //mTbBstFlfg.Cid = Convert.ToDecimal(context.Request.Form["texCid"]); //父级表相关联ID
                    mTbBstFlfg.Flfgxgsj = DateTime.Now; //最后修改时间
                    mTbBstFlfg.Sfsc = "0"; //是否删除 0 未删除  1删除
                    mTbBstFlfg.Sfrd = context.Request.Form["selEnabled"]; //是否热点 1热点   0非热点

                    mResultModel = TbBstFlfgBLL.Update(mTbBstFlfg, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;

                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            string json = "";
            try
            {
                TbBstFlfg mTbBstFlfg = new TbBstFlfg(); //声明实体
                TbBstDic mBaseDic = new TbBstDic();
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mTbBstFlfg = TbBstFlfgBLL.Load(paramKey); //根据键值获取管理员
                    if (mTbBstFlfg != null)
                    {
                        IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
                        json = JsonConvert.SerializeObject(mTbBstFlfg, dtConverter);
                        //解析是否热点
                        if (!String.IsNullOrEmpty(mTbBstFlfg.Sfrd))
                        {
                            var list = TbBstBsznBLL.GetDicByCodeAndParentnode(mTbBstFlfg.Sfrd, "bool");
                            mBaseDic = list.SingleOrDefault();
                            Hashtable hashtable = new Hashtable();
                            if (mBaseDic != null)
                            {
                                hashtable.Add("Vsfrd", mBaseDic.Description);
                                json = JsonString.AddAttribute(json, hashtable);
                            }
                        }
                        //解析法律法规分类
                        if (!String.IsNullOrEmpty(mTbBstFlfg.Cid.ToString()))
                        {
                            var list = TbBstBsznBLL.GetDicByCodeAndParentnode(mTbBstFlfg.Cid.ToString(), "flfg");
                            mBaseDic = list.SingleOrDefault();
                            Hashtable hashtable = new Hashtable();
                            if (mBaseDic != null)
                            {
                                hashtable.Add("VCID", mBaseDic.Description);
                                json = JsonString.AddAttribute(json, hashtable);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            string name = context.Request.Form["txtName"];
            string type = context.Request.Form["selType"];
            string ztfl = context.Request.Form["txtZTFL"];
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
                return;
            DataTable dtTbBstFlfg = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            QueryByPage Q = new QueryByPage();
            QueryInfo qInfo = new QueryInfo();

            SqlConditionList sqllist = new SqlConditionList();
            if (!string.IsNullOrEmpty(name))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, TbBstFlfgConst.Columns.Xxmc, '%' + name + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(ztfl))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, TbBstFlfgConst.Columns.Ztfl, '%' + ztfl + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (type != "请选择" && !string.IsNullOrEmpty(type))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstFlfgConst.Columns.Cid, type), SqlLogicOper.AND_LOGIC);
            }
            sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstFlfgConst.Columns.Sfsc, "0"), SqlLogicOper.AND_LOGIC);   //0未删除  1删除
            dtTbBstFlfg = Q.GetRecordByPage("*", sqllist.ToSql(), "", intPageSize, intPageIndex, TbBstFlfgConst.QTableName, TbBstFlfgConst.FieldKey, "").Tables[0];
            if (dtTbBstFlfg != null && dtTbBstFlfg.Rows.Count > 0)
            {
                for (int i = 0; i < dtTbBstFlfg.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtTbBstFlfg.Columns[i].Caption;
                    list.Add(hm);
                }
                tm.Header = list;
                tm.data = dtTbBstFlfg;
                tm.totalCount = Convert.ToString((int)Q.RowCount);
            }
            string json = JsonConvert.SerializeObject(tm);
            context.Response.Write(json);
        }
        #endregion

    }
}


